//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    top: [{
        'title': '商品',
        'active': true,
        'type': 0
      },
      {
        'title': '商家',
        'active': false,
        'type': 1
      }
    ],
    imgUrl: baseJs.imgUrl,
    startX: 0, //开始坐标
    startY: 0,
    type: 0,
    pageIndex: 1,
    state: "",
    items: [],
    // 
    uid:"",

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      uid: wx.getStorageSync('uid'),
    })
    this.GetGoodsCollection();
  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    var type = this.data.type;
    this.data.pageIndex++;
    if (type == 0) {
      this.GetGoodsCollection();
    } else {
      this.GetBusinessCollection();
    }

  },

  //手指触摸动作开始 记录起点X坐标
  touchstart: function(e) {
    //开始触摸时 重置所有删除
    this.data.items.forEach(function(v, i) {
      //只操作为true的
      if (v.isTouchMove)
        v.isTouchMove = false;
    })
    this.setData({
      startX: e.changedTouches[0].clientX,
      startY: e.changedTouches[0].clientY,
      items: this.data.items
    })
  },
  //滑动事件处理
  touchmove: function(e) {
    var that = this,
      //当前索引
      index = e.currentTarget.dataset.index,
      //开始坐标
      startX = that.data.startX,
      startY = that.data.startY,
      //滑动变化坐标
      touchMoveX = e.changedTouches[0].clientX,
      touchMoveY = e.changedTouches[0].clientY,
      //获取滑动角度
      angle = that.angle({
        X: startX,
        Y: startY
      }, {
        X: touchMoveX,
        Y: touchMoveY
      });
    that.data.items.forEach(function(v, i) {
      v.isTouchMove = false
      //滑动超过30度角 return
      if (Math.abs(angle) > 30) return;
      if (i == index) {
        if (touchMoveX > startX)
          //右滑
          v.isTouchMove = false
        else //左滑
          v.isTouchMove = true
      }
    })
    //更新数据
    that.setData({
      items: that.data.items
    })
  },

  /**
  
  * 计算滑动角度
  
  * @param {Object} start 起点坐标
  
  * @param {Object} end 终点坐标
  
  */
  angle: function(start, end) {
    var _X = end.X - start.X,
      _Y = end.Y - start.Y
    //返回角度 /Math.atan()返回数字的反正切值
    return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
  },
  // 
  topTap: function(e) {
    var top = this.data.top;
    var type = e.currentTarget.dataset.type;
    var pageIndex = this.data.pageIndex;
    var items = this.data.items;
    for (var i = 0, len = top.length; i < len; ++i) {
      if (top[i].type == type) {
        top[i].active = true;
      } else {
        top[i].active = false
      }
    }
    this.setData({
      top: top,
      type: type,
      pageIndex: 1,
      items: [],
    })
    if (type == 0) {
      this.GetGoodsCollection();
    } else {
      this.GetBusinessCollection();
    }
  },
  // 收藏商品
  GetGoodsCollection: function() {
    var that = this;
    var uid = this.data.uid;
    var pageIndex = this.data.pageIndex;
    var items = this.data.items;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetGoodsCollection&PageSize=10&UID=" + uid + "&PageIndex=" + pageIndex),
      success: function(res) {
        var json = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        for (var i = 0; i < json.length; i++) {
          items.push(json[i])
        }
        for (var i = 0; i < items.length; i++) {
          items[i].CreateTime = items[i].CreateTime.replace('T', ' ');
        }
        that.setData({
          items: items,
        })
        console.log(items);
        if (json.length == 10) {
          that.setData({
            state: 'loadMore',
          })
        } else if (json.length == 0 && pageIndex == 1) {
          that.setData({
            state: 'noData',
          })
        } else if (json.length < 10) {
          that.setData({
            state: 'noMore',
          })
        } else if (json.length == 0 && pageIndex > 1) {
          that.setData({
            state: 'noMore',
          })
        }
      }
    })

  },
  // 收藏商家
  GetBusinessCollection: function() {
    var that = this;
    var uid = this.data.uid;
    var pageIndex = this.data.pageIndex;
    var items = this.data.items;
    wx.request({
      url: baseJs.api + baseJs.encryUrl("method=GetBusinessCollection&PageSize=10&UID=" + uid + "&PageIndex=" + pageIndex),
      success: function(res) {
        var json = JSON.parse(baseJs.decryUrl(res.data)).ResultData;
        for (var i = 0; i < json.length; i++) {
          items.push(json[i])
        }
        for (var i = 0; i < items.length; i++) {
          items[i].CreateTime = items[i].CreateTime.replace('T', ' ');
        }
        that.setData({
          items: items,
        })
        console.log(items);
        if (json.length == 10) {
          that.setData({
            state: 'loadMore',
          })
        } else if (json.length == 0 && pageIndex == 1) {
          that.setData({
            state: 'noData',
          })
        } else if (json.length < 10) {
          that.setData({
            state: 'noMore',
          })
        } else if (json.length == 0 && pageIndex > 1) {
          that.setData({
            state: 'noMore',
          })
        }
      }
    })
  },
  // del
  del: function(e) {
    var that = this;
    var uid = this.data.uid;
    var id = e.currentTarget.dataset.id;
    var type = this.data.type;
    var items = this.data.items;
    if (type == 0) {
      var str = "method=DeleteGoodsCollection&UID=" + uid + "&SCID=" + id;
    } else {
      var str = "method=DeleteBusinessCollection&UID=" + uid + "&SCID=" + id;
    }
    wx.showModal({
      title: '提示',
      content: '确认删除该收藏？',
      success: function(res) {
        console.log(str);
        if (res.confirm) {
          wx.request({
            url: baseJs.api + baseJs.encryUrl(str),
            success: function(res) {
              var json = JSON.parse(baseJs.decryUrl(res.data));
              if (json.ResultFlag == 1) {
                wx.showToast({
                  title: json.ResultMsg,
                  icon: 'none',
                  duration: 2000
                });
                items.splice(e.currentTarget.dataset.index, 1);
                that.setData({
                  items: items
                })
              } else {
                wx.showToast({
                  title: json.ResultMsg,
                  icon: 'none',
                  duration: 2000
                });
              }
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })


  },
  // 
})